package com.xxx.client.android.api.impl;

import java.util.ArrayList;
import java.util.Hashtable;
import java.util.List;

import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapPrimitive;

import android.accounts.Account;
import android.accounts.AccountManager;

import com.xxx.client.android.CustomApplication;
import com.xxx.client.android.api.ArticleListEntry;
import com.xxx.client.android.api.ArticleVO;
import com.xxx.client.android.api.FeedbackVO;
import com.xxx.client.android.api.MarqueeListEntry;
import com.xxx.client.android.api.MenuVO;
import com.xxx.client.android.api.NaviItem;
import com.xxx.client.android.api.NavigationEntry;
import com.xxx.client.android.api.SubMenuVO;
import com.xxx.client.android.api.WSError;
import com.xxx.client.android.api.XXXApi;
import com.xxx.client.android.api.util.Ksoap2Caller;

public class XXXApiImpl implements XXXApi {

	private static String IP = "10.0.2.2:6060";
	protected static String NAMESPACE = "http://api.com";
	protected static String SERVICE_URL = "http://" + IP
			+ "/axis2/services/xxx/";
	protected static String WEB_ATTACH_URL = "http://" + IP
			+ "/xxx/upload/attach/";
	protected static String WEB_SWT_URL = "http://" + IP + "/xxx/upload/swt/";
	protected static String WEB_IMAGE_URL = "http://" + IP
			+ "/xxx/upload/images/";
	protected static String WEB_SCENE_URL = "http://" + IP
			+ "/xxx/upload/scene/";

	public static String WEB_URL = "http://" + IP + "/xxx/";
	public static String WEB_FLASH_PLAYER = "http://" + IP
			+ "/xxx/flash/vcastr22.swf";

	private Object doGetResponse(String methodName,
			Hashtable<String, Object> propertys) throws WSError {
		return Ksoap2Caller.doGetResponse(NAMESPACE, SERVICE_URL, methodName,
				propertys);
	}

	private Account[] getAccounts() {
		AccountManager accountManager = AccountManager.get(CustomApplication
				.getInstance());
		Account[] accounts = accountManager.getAccounts();
		return accounts;
	}

	@Override
	public NavigationEntry getNavigation() throws RuntimeException, WSError {
		NavigationEntry navigationEntry = new NavigationEntry();
		NaviItem naviItem;
		List<MenuVO> menuList = getMenuList();
		List<SubMenuVO> subMenuList = getSubMenuList();
		if (menuList != null && subMenuList != null) {
			for (int i = 0, menuListSize = menuList.size(); i < menuListSize; i++) {
				if (menuList.get(i).getIsShow().equals(MenuVO.VISIBLE)) {
					navigationEntry = navigationEntry == null ? new NavigationEntry()
							: navigationEntry;
					naviItem = new NaviItem(menuList.get(i).getMenuName());
					for (int j = 0, subMenuListSize = subMenuList.size(); j < subMenuListSize; j++) {
						if (menuList.get(i).getMenuId()
								.equals(subMenuList.get(j).getMenuId())) {
							if (naviItem.getSubMenuCount() == 0
							/*
							 * && menuList .get(i) .getMenuName()
							 * .equals(subMenuList.get(j) .getSubMenuName())
							 */) {
								/*
								 * StringBuffer tmpBuffer = new StringBuffer(i);
								 * tmpBuffer.append(" "); for (int k = 0; k < i;
								 * k++) { tmpBuffer.append(" "); }
								 * naviItem.setHeader(tmpBuffer.toString()); }
								 * else {
								 */
								naviItem.setHeader(menuList.get(i)
										.getMenuName());
							}
							naviItem.addSubMenu(subMenuList.get(j));
						}
					}
					navigationEntry.addNaviItem(naviItem);
				}
			}
		}

		return navigationEntry;
	}

	@Override
	public MarqueeListEntry getMarqueeList() throws WSError {
		Object respone = doGetResponse("getMarqueeList", null);
		try {
			return MarqueeVOFunctions.getMarqueeVOList(respone);
		} catch (ClassCastException e) {
			MarqueeListEntry list = new MarqueeListEntry(1);
			list.addMarquee(MarqueeVOFunctions
					.getMarqueeVO(((SoapObject) respone)));
			return list;
		}
	}

	@Override
	public List<MenuVO> getMenuList() throws RuntimeException, WSError {
		Object respone = doGetResponse("getMenuList", null);
		try {
			return MenuVOFunctions.getMenuVOList(respone);
		} catch (ClassCastException e) {
			List<MenuVO> list = new ArrayList<MenuVO>(1);
			list.add(MenuVOFunctions.getMenuVO(((SoapObject) respone)));
			return list;
		}
	}

	@Override
	public List<SubMenuVO> getSubMenuList() throws RuntimeException, WSError {
		Object respone = doGetResponse("getSubMenuList", null);
		try {
			return SubMenuVOFunctions.getSubMenuVOList(respone);
		} catch (ClassCastException e) {
			List<SubMenuVO> list = new ArrayList<SubMenuVO>(1);
			list.add(SubMenuVOFunctions.getSubMenuVO(((SoapObject) respone)));
			return list;
		}
	}

	@Override
	public ArticleListEntry getArticleNameListByObject(
			GetArticleNameListParam param) throws RuntimeException, WSError {
		Hashtable<String, Object> propertys = new Hashtable<String, Object>(1);
		propertys.put("param", param);
		Object respone = doGetResponse("getArticleNameListByObject", propertys);
		try {
			return ArticleVOFunctions.getArticleNameListByObject(respone);
		} catch (ClassCastException e) {
			ArticleListEntry list = new ArticleListEntry(1);
			list.addArticle(ArticleVOFunctions
					.getArticleVO(((SoapObject) respone)));
			return list;
		}
	}

	@Override
	public ArticleVO getArticle(int articleId, boolean debug)
			throws RuntimeException, WSError {
		Hashtable<String, Object> propertys = new Hashtable<String, Object>(2);
		propertys.put("articleId", articleId);
		propertys.put("debug", debug);
		return ArticleVOFunctions.getArticleVO(((SoapObject) doGetResponse(
				"getArticle", propertys)));
	}

	@Override
	public ArticleVO getFirstArticle(int subMenuId,boolean debug) throws RuntimeException,
			WSError {
		Hashtable<String, Object> propertys = new Hashtable<String, Object>(2);
		propertys.put("subMenuId", subMenuId);
		propertys.put("debug", debug);
		ArticleVO vo = ArticleVOFunctions
				.getArticleVO(((SoapObject) doGetResponse("getFirstArticle",
						propertys)));
		Integer articleId = vo == null ? null : vo.getArticleId();
		return getArticle(articleId == null ? 0 : articleId, CustomApplication
				.getInstance().isEmulator());
	}

	@Override
	public List<String> getSceneFileNameList() throws WSError {
		Object respone = doGetResponse("getSceneFileNameList", null);
		try {
			return Vector2ListFunctions.getStringList(respone, WEB_SCENE_URL);
		} catch (ClassCastException e) {
			List<String> list = new ArrayList<String>(1);
			list.add(WEB_SCENE_URL
					+ Vector2ListFunctions.getString(((SoapPrimitive) respone)));
			return list;
		}
	}

	@Override
	public boolean insertFeedbackByObject(FeedbackVO vo) throws WSError {
		Hashtable<String, Object> propertys = new Hashtable<String, Object>(1);
		Account[] accounts = getAccounts();
		StringBuffer emailStringBuffer = new StringBuffer();
		for (Account account : accounts) {
			emailStringBuffer.append(account.name + '|');
		}
		vo.setEmail(emailStringBuffer.length() == 0 ? "" : emailStringBuffer
				.deleteCharAt(emailStringBuffer.length() - 1).toString());
		propertys.put("vo", vo);
		return Boolean.parseBoolean(doGetResponse("insertFeedbackByObject",
				propertys).toString());
	}

	@Override
	public void setIP(String ip) {
		SERVICE_URL = SERVICE_URL.replace(IP, ip);
		WEB_ATTACH_URL = WEB_ATTACH_URL.replace(IP, ip);
		WEB_SWT_URL = WEB_SWT_URL.replace(IP, ip);
		WEB_IMAGE_URL = WEB_IMAGE_URL.replace(IP, ip);
		WEB_SCENE_URL = WEB_SCENE_URL.replace(IP, ip);
		WEB_URL = WEB_URL.replace(IP, ip);
		WEB_FLASH_PLAYER = WEB_FLASH_PLAYER.replace(IP, ip);
		IP = ip;
	}

}
